package com.projects.xenia.add_activities;

import java.util.Calendar;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.projects.xenia.R;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.CustomersRefObject;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class Activity_Map extends FragmentActivity {
 
	private GoogleMap googleMap;
	
	private CustomersRefObject CustomerObj;
	
	private ImageView _backward;
	private ImageView _gibrid;
	private ImageView _standart;
	
	
	private Marker SetRoutePoint (String exclude_customer_id) {
		
		Calendar C = Calendar.getInstance();		
		int dayoftroute = C.get(Calendar.DAY_OF_WEEK)-1;
		Marker _lastmarker = null;
		
		Cursor RouteCursor = MainGlobalModule.DBE.rawQuery("select customers_id from xref_Routes where dayofroute = ? order by position", new String [] {String.valueOf(dayoftroute)});
		if (RouteCursor.getCount()>0) {
			RouteCursor.moveToFirst();
			while (!RouteCursor.isAfterLast()) {				
				String customer_id = RouteCursor.getString(0);
				if (!customer_id.equals(exclude_customer_id) && CustomerObj.GetCustomerById(customer_id)) {
					if (CustomerObj.RefItem.lat!=CustomerObj.RefItem.lon) {
						LatLng latLng = new LatLng(CustomerObj.RefItem.lat, CustomerObj.RefItem.lon);
						MarkerOptions _markeroptions = new MarkerOptions();
						_markeroptions.position(latLng);
						_markeroptions.title(CustomerObj.GetCustomersDescrLong());
						_markeroptions.snippet(CustomerObj.RefItem.outletadress);
						_markeroptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
						Marker _mymarker = googleMap.addMarker(_markeroptions);
						_lastmarker = _mymarker;
					}
				}
				RouteCursor.moveToNext();
			}
		}
		
		return (_lastmarker);
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		Marker _lastmarker;
		
		setContentView(R.layout.map_form);		
		
		googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
		googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);		
		googleMap.setMyLocationEnabled(true);
		googleMap.getUiSettings().setCompassEnabled(true);
	    googleMap.getUiSettings().setZoomControlsEnabled(true);
	    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
	    googleMap.getUiSettings().setZoomControlsEnabled(true);   
	    
		
		Bundle DocExtra = getIntent().getExtras();
		if (DocExtra!=null){
			
			CustomerObj = new CustomersRefObject();
			
			String customer_id = DocExtra.getString("customer_id");
			_lastmarker = SetRoutePoint (customer_id);
			
			if (CustomerObj.GetCustomerById(customer_id)) {
				
				if (CustomerObj.RefItem.lat!=CustomerObj.RefItem.lon) {
					LatLng latLng = new LatLng(CustomerObj.RefItem.lat, CustomerObj.RefItem.lon);
					MarkerOptions _markeroptions = new MarkerOptions();
					_markeroptions.position(latLng);
					_markeroptions.title(CustomerObj.GetCustomersDescrLong());
					_markeroptions.snippet(CustomerObj.RefItem.outletadress);
					_markeroptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
					Marker _mymarker = googleMap.addMarker(_markeroptions);
					_mymarker.showInfoWindow();
					googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));					
				}
				else
				{
					Toast.makeText(this, "Координаты не определены !!!", Toast.LENGTH_SHORT).show();
					if (_lastmarker!=null) {
						googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(_lastmarker.getPosition(), 10.0f));
					}
					else
					{
						Toast.makeText(this, "Координаты маршрута не определены !!!", Toast.LENGTH_SHORT).show();
					}
				}
				
			} 
		}
		
		_backward = (ImageView)findViewById(R.id.map_BackWard);
		_backward.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		_gibrid = (ImageView)findViewById(R.id.map_Gibrid);
		_gibrid.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
			}
		});
		
		_standart = (ImageView)findViewById(R.id.map_Standart);
		_standart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
			}
		});
		
	        
	}
 

} 



